package com.dofast.bt.mvp.view.activity.login;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.mvp.presenter.login.LoginPresenter;
import com.dofast.bt.mvp.view.activity.main.MainActivity;
import com.dofast.bt.util.Helper;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.ChartData;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;


/**
 * username:Created by hsj on 2016/10/11 0011.
 * class：
 * 类描述：登入界面
 */

public class LoginActivity extends BaseMvpActivity<LoginPresenter, LoginView> implements LoginView, View.OnClickListener {


    @BindView(R.id.et_username)
    EditText username;
    @BindView(R.id.et_pwd)
    EditText password;
    @BindView(R.id.btn_login)
    Button login;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.column)
    ColumnChartView column;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_login;
    }

    @Override
    protected void addOnClickListener() {
        login.setOnClickListener(this);
    }

    @Override
    protected void initView() {
//        presenter.initData();
        presenter.autoLogin();
        List<AxisValue> axisXValues = new ArrayList<AxisValue>();
        //添加数据
        List<SubcolumnValue> values = new ArrayList<SubcolumnValue>();
        values.add(new SubcolumnValue(100, Color.rgb(255, 255, 255)));
        values.add(new SubcolumnValue(50, Color.rgb(10, 0, 22)));
        values.add(new SubcolumnValue(120, Color.rgb(22, 155, 0)));

        axisXValues.add(new AxisValue(50));


//添加柱状图
        Column one = new Column(values);
        List<Column> list = new ArrayList<Column>();
        list.add(one);

        ColumnChartData data = new ColumnChartData(list);
        data.setAxisXBottom(new Axis(axisXValues).setHasLines(true).setTextColor(Color.BLACK).setName("学历").setHasTiltedLabels(true).setMaxLabelChars(8));
        //属性值含义同X轴
        data.setAxisYLeft(new Axis().setHasLines(true).setName("人数").setTextColor(Color.BLACK).setMaxLabelChars(5));
//显示柱状图
        column.setColumnChartData(data);
    }

    /**
     * 创建LoginPresenter实例
     *
     * @param id
     * @param args
     * @return
     */
    @Override
    public Loader<LoginPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<LoginPresenter>() {
            @Override
            public LoginPresenter create() {
                return new LoginPresenter();
            }
        });
    }

    @Override
    public String getUsername() {
        return username.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return password.getText().toString().trim();
    }

    @Override
    public void showResult(String result) {
        Helper.showToast(result);
    }

    @Override
    public void autoLogin(String user, String pwd) {
        username.setText(user);
        password.setText(pwd);
    }

    @Override
    public void showLogo(String url) {
        Glide.with(this).load(url).placeholder(R.mipmap.icon_photo_no).into(ivLogo);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
//                presenter.login();//登录操作
                gotoActivity(MainActivity.class, true);
                break;
        }
    }


    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {
        startActivity(tClass, isFinish);
    }

    @Override
    public void initTitle() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}